﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using MyLibrary.ThreeLayer.DataAccess.Interface;
using MyLibrary.ThreeLayer.DataAccess.MetaData;

namespace MyLibrary.ThreeLayer.Utility
{
    internal class FullMatchEntityMapper<T> : IEntityMapper<T> where T : class, new()
    {
        public void FillEntity(IDataReader reader, MappingCollection collection, T data)
        {
            foreach (MappingField field in collection)
                field.SetValue(data, reader[field.ColumnName]);
        }

        public IList<T> ToEntities(IDataReader reader, MappingCollection collection)
        {
            List<T> list = new List<T>();
            while (reader.Read())
                list.Add(this.ToEntity(reader, collection));
            return list;
        }

        public T ToEntity(IDataReader reader, MappingCollection collection)
        {
            T entity = Activator.CreateInstance<T>();
            foreach (MappingField field in collection)
                field.SetValue(entity, reader[field.ColumnName]);
            return entity;
        }
    }
}
